package cn.itcast.GsyVideoPlayer;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;

import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.google.gson.Gson;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;

import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import com.shuyu.gsyvideoplayer.video.base.GSYVideoControlView;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoViewBridge;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class GSYVideoPlayerActivity extends AppCompatActivity {


    private static final String TAG ="GSYVideoPlayerActivity";

    private StandardGSYVideoPlayer standardPlayer;

    private Button btnStart,btnStop,obtain,btn_resetting,btn_cache;

    private Switch sc_Looping;

    private TextView tv_Looping,tv_count;


    private ZoomControls mZC;


    private GSYVideoControlView gsyVideoControlView;


    private static int pauseSize= 0 ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //去除标题导航栏
        getSupportActionBar().hide();

        //去除时间和电量等
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gsyvideo_player2);

        standardPlayer = findViewById(R.id.standardPlayer);

        btnStart = findViewById(R.id.btnStart);

        btnStop=findViewById(R.id.btnStop);

        obtain=findViewById(R.id.obtain);

        btn_resetting=findViewById(R.id.btn_resetting);

        sc_Looping=findViewById(R.id.sc_Looping);

        mZC=findViewById(R.id.zcm);

        tv_Looping=findViewById(R.id.tv_Looping);

        tv_count=findViewById(R.id.tv_count);

        btn_cache=findViewById(R.id.btn_cache);


        // 视频链接和视频标题
        standardPlayer.setUp("https://media.w3.org/2010/05/sintel/trailer.mp4", true, "GSY视频播放器");


        // 加载视频按钮
        btnStart.setOnClickListener(view -> {

            standardPlayer.startPlayLogic();

            standardPlayer.getCurrentVideoWidth();

            standardPlayer.getCurrentVideoHeight();

            standardPlayer.getCurrentVideoWidth();//获取当前视频的宽度

            standardPlayer.getCurrentVideoHeight();//获取当前视频的高度


            Log.e("获取当前视频的宽度",""+ standardPlayer.getCurrentVideoWidth());


            Log.e("获取当前视频的高度",""+standardPlayer.getCurrentVideoHeight());


        });


         btnStop.setOnClickListener(view -> {


             standardPlayer.onVideoPause();


             Log.e("",""+standardPlayer.getCurrentVideoWidth());


             Log.e("获取当前视频的宽度","测试"+standardPlayer.getCurrentVideoWidth());


             Log.e("获取当前视频高度","测试"+standardPlayer.getCurrentVideoWidth());


             Log.e("获取当前视频的宽度","测试"+standardPlayer);


         });


//          btn_cache.setOnClickListener(view -> {
//
//
//          Log.e("获取当前缓冲进度","测试"+ standardPlayer.getBuffterPoint());
//
//
//          Log.e("获取判断当前是否正在播放","测试"+standardPlayer.isInPlayingState());
//
//
//          Log.e("获取","测试");
//
//
//          });


             mZC.setIsZoomInEnabled(true);


             mZC.setIsZoomOutEnabled(true);


             mZC.setOnZoomInClickListener(view -> {


                 pauseSize+=2;


                 tv_count.setTextSize(pauseSize);


             });


             mZC.setOnZoomOutClickListener(view -> {


        if (pauseSize>0){


                 pauseSize-=2;


                 tv_count.setTextSize(pauseSize);


                 }


             });

             /*
             *
             *
             * 第三步：配置虚拟设备的网络连接
             *
             * 在虚拟设备启动后，在菜单栏中选择“View” -> “Tool Windows” -> “Device File Explorer”。这将打开一个文件浏览器窗口。

               在文件浏览器中，导航到“/system”文件夹下，找到并双击打开“build.prop”文件。

               在“build.prop”文件中，找到并双击编辑“dns1”和“dns2”属性。将它们的值更改为你主机电脑上的DNS服务器地址。保存更改后，关闭文件。

               接下来，在Android Studio的工具栏上，找到并点击“AVD Manager”按钮。选中你的虚拟设备，然后点击右侧的下拉菜单按钮。选择“Wipe Data”选项来清除虚拟设备上的数据。

               最后，重新启动虚拟设备并检查是否已解决无法上网的问题。

*               好好看，

             *
             * */

         obtain.setOnClickListener(view -> {

             Toast.makeText(GSYVideoPlayerActivity.this,"获取当前播放进度"+standardPlayer.getCurrentPositionWhenPlaying(),Toast.LENGTH_SHORT).show();

             Log.e("获取当前播放进度",""+standardPlayer.getCurrentPositionWhenPlaying());

             Log.e("获取总时间",""+standardPlayer.getDuration());


         });


         btn_resetting.setOnClickListener(view -> {


             standardPlayer.onVideoReset();//重置


         });


           //开启循环
         sc_Looping.setOnCheckedChangeListener((compoundButton, b) -> {


             if (b==true){

                 if (standardPlayer.isInPlayingState()==false){

                     standardPlayer.setLooping(true);//是否循环播

                     tv_Looping.setText("已开启循环");

                     Toast.makeText(GSYVideoPlayerActivity.this,"已开启循环",Toast.LENGTH_SHORT).show();

                 }


             }else {


                 standardPlayer.setLooping(false);//是否循环播


                 tv_Looping.setText("已关闭循环");


                 Toast.makeText(GSYVideoPlayerActivity.this,"已关闭循环",Toast.LENGTH_SHORT).show();


             }


    });


//        standardPlayer.onAutoCompletion();
//        standardPlayer.onCompletion();//播放完成
//        standardPlayer.setLooping(true);//是否循环播
//        standardPlayer.setIfCurrentIsFullscreen(true);//判断是否全屏
//        standardPlayer.getCurrentState();//获取当前播放状态
//        standardPlayer.getBuffterPoint();//缓冲进度/缓存进度
//        standardPlayer.isInPlayingState();//根据状态判断是否播放中
//        standardPlayer.startPlayLogic();//开始播放
//        standardPlayer.getDuration();//获取当前总时长
//        standardPlayer.getCurrentPositionWhenPlaying();//获取当前播放进度
//        standardPlayer.clearCurrentCache();//清理缓存
//        standardPlayer.onVideoPause();//暂停状态
//        standardPlayer.onVideoReset();//重置
//        standardPlayer.getCurrentVideoWidth();//获取当前视频的宽度
//        standardPlayer.getCurrentVideoHeight();//获取当前视频的高度
//        standardPlayer.release();//清理


        OrientationUtils orientationUtils = new OrientationUtils(this, standardPlayer);


        //视频全屏播放
        standardPlayer.getFullscreenButton().setOnClickListener(view -> {


            orientationUtils.resolveByClick();


        });

    }



    @Override
    protected void onRestart() {
        super.onRestart();



    }


    @Override
    protected void onPause() {
        super.onPause();

        standardPlayer.onVideoPause();

    }


    @Override
    protected void onResume() {
        super.onResume();

        standardPlayer.onVideoResume();

    }


    @Override
    protected void onDestroy() {

        super.onDestroy();

        GSYVideoManager.releaseAllVideos();

        GSYVideoPlayerActivity.this.finish();

    }


    @Override
    public void onBackPressed() {


        super.onBackPressed();


        //释放所有
        standardPlayer.setVideoAllCallBack(null);


    }



}

